package com.handmark.pulltorefresh.library.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.handmark.pulltorefresh.library.R;


/**
 * 任继波
 * Created by Administrator on 2017/2/22.
 */

public class MyView extends View {

    private Paint mPaint;
    private Bitmap bitTwo;
    private int x=0;
    private int y=0;
    private int x1=0;
    private int y1=0;
    private Bitmap scaledBitmap1;
    private Bitmap scaledBitmap;
    private boolean boo=true;
    private int count;

    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        bitTwo = BitmapFactory.decodeResource(getResources(), R.drawable.loading_double_1);
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.img_mama);
        scaledBitmap1 = Bitmap.createScaledBitmap(bitmap, dip2px(context,50), dip2px(context,50), true);

        Bitmap bitmap1= BitmapFactory.decodeResource(getResources(),R.drawable.img_baby);
        scaledBitmap = Bitmap.createScaledBitmap(bitmap1, dip2px(context,49), dip2px(context,49), true);
    }
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.x1=w-scaledBitmap1.getWidth()*2;
        count = w;
        this.y1=h/2;
//        Log.d("zzz","zzzzzzzz");

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        Log.d("zzz","x:"+x+"   y:"+y);
        mPaint.setColor(Color.RED);
        canvas.save();

        canvas.drawBitmap(scaledBitmap1,x,y,mPaint);
        canvas.restore();

        canvas.save();

        canvas.drawBitmap(scaledBitmap,getWidth()-x-scaledBitmap.getWidth(),y,mPaint);
        canvas.restore();
    }
    public void setXY(float x,float y){

            this.x = (int) (x1 * x)/2;
            this.y = (int) (y1 * y);

    }
    /**
     * 测量方法
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }
    //测量宽度
    private int measureWidth(int widthMeasureSpec){
        int result = 0;
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        if (MeasureSpec.EXACTLY == mode) {
            result = size;
        }else {
            result = bitTwo.getWidth();
            if (MeasureSpec.AT_MOST == mode) {
                result = Math.min(result, size);
            }
        }
        return result;
    }
    //测量高度
    private int measureHeight(int heightMeasureSpec){
        int result = 0;
        int size = MeasureSpec.getSize(heightMeasureSpec);
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        if (MeasureSpec.EXACTLY == mode) {
            result = size;
        }else {
            result = bitTwo.getHeight();
            if (MeasureSpec.AT_MOST == mode) {
                result = Math.min(result, size);
            }
        }
        return result;
    }
}
